package com.first.controller;



import com.first.dao.DeptDao;
import com.first.dao.RoleDao;
import com.first.entity.Deptn;
import com.first.entity.User;
import com.first.service.impl.UserServiceImpl;
import com.first.util.LayTab;
import com.first.util.LayuiImg;
import com.first.util.Onload;
import com.first.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Resource
    private DeptDao deptDao;

    private String url;

    /**
     * 用户登录验证功能功能
     * @param username
     * @param password
     * @param req
     * @return
     */
    @RequestMapping("/login")
    public Result  login(String username, String password, HttpServletRequest req){
        User u = userService.authentication(username,password);
        Result result = new Result();
        if(u==null){
            result.setCode(404);
            result .setMsg("账号或者密码错误！！！");
        }else if(u.getStatus()==1){
            result.setCode(404);
            result .setMsg("你已离职！！！");
        }else if(u.getStatus()==2){
            result.setCode(404);
            result .setMsg("你已被停职！！！");
        }else if (u.getRoleStatus()==0) {
            result.setCode(404);
            result .setMsg("你还没有权限！！！");
        }else if(u.getDeptStatus()==0){
            result.setCode(404);
            result .setMsg("你所在的部门已被停用！！！");
        }else {
            HttpSession session = req.getSession();
            session.setAttribute("user",u);
            result.setCode(200);
            result .setMsg("登录成功》》》正在转跳");
        }
        return result;
    }

    /**
     * 新增用户主要信息
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public Result addUser(User user){
        int i = userService.addMainUser(user);
        Result result = new Result();
        if(i>0){
            result.setCode(1);
            result.setMsg("添加成功");
        }else {
            result.setCode(0);
            result.setMsg("添加失败");
        }
        return result;
    }


    /**
     * 查询全部用户信息，联合模糊查询
     * @param user
     * @return
     */
    @RequestMapping("/userTab")
    public LayTab queryAll(User user){
        List<Map> list = userService.queryAllUser(user);
        int count = userService.queryAllUserCount();
        if(list!=null){
            LayTab layTab = new LayTab(count,list);
            return layTab;
        }
        return null;
    }

    /**
     * 获取所有部门信息
     * @return
     */
    @RequestMapping("/dept")
    public List getDept(Deptn deptn){
        List<Map> list = deptDao.queryAll();
        return list;
    }

    /**
     * 单图片上传
     * @param file
     * @return
     */

    @RequestMapping("/img")
    public LayuiImg onload(MultipartFile file){
        url = Onload.getURL(file);
        List<String> list = new ArrayList<>();
        list.add(url);
        LayuiImg layuiImg = new LayuiImg(list);
        return layuiImg;
    }
    /**
     * 用户修改详细信息
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public Result userInfo(@RequestBody User user){
        user.setUrl(url);
        System.out.println(url);
        int i = userService.update(user);
        Result result = new Result();
        if(i>0){
            result.setCode(1);
            result.setMsg("修改成功");
        }else {
            result.setCode(0);
            result.setMsg("修改失败");
        }
        return result;
    }
    /**
     * 修改用户信息/添加新用户信息
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    public Result user(User user){
        Result result = new Result();
        if(user.getId()!=null){
            int i = userService.updateUser(user);
            if(i>0){
                result.setCode(1);
                result.setMsg("修改成功");
            }else {
                result.setCode(0);
                result.setMsg("修改失败");
            }
        }else {
            int i = userService.addMainUser(user);
            if(i==-2){
                result.setCode(0);
                result.setMsg("用户名或身份证号已存在!!");
            }else if(i>0){
                result.setCode(1);
                result.setMsg("添加成功");
            }else {
                result.setCode(0);
                result.setMsg("添加失败");
            }
        }
        return result;
    }

    /**
     * 获得离职人员信息
     * @return
     */
    @RequestMapping("Resign")
    public LayTab getResign(){
        List<Map> resign = userService.getResign();
        LayTab layTab = new LayTab(resign.size(),resign);
        return layTab;
    }

    /**
     *销毁个人信息
     * @param request
     */
    @RequestMapping("quit")
    public void quit(HttpServletRequest request,HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.invalidate();
        try {
            response.sendRedirect("../login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @RequestMapping("resetting")
    public Result resettingPassword(Integer id){
        int i = userService.resetPasswordById(id);
        Result result = new Result();
        if(i>0){
            result.setCode(1);
            result.setMsg("重置完成");
        }else {
            result.setCode(0);
            result.setMsg("重置失败");
        }
        return result;
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @param id
     * @return
     */
    @RequestMapping("change")
    public Result changePassword(String oldPassword,String newPassword,Integer id){
        int i = userService.changePassword(oldPassword,newPassword,id);
        Result result = new Result();
        if(i>0){
            result.setCode(1);
            result.setMsg("新密码已生效");
        }else {
            result.setCode(0);
            result.setMsg("修改失败");
        }
        return result;
    }

}
